package com.blockmeta.bbs.businesslibrary.widget.chartview.Axis;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;


import com.blockmeta.bbs.businesslibrary.widget.chartview.CoinChart;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Layer.Layer;

import java.util.List;

public class XAxis extends Axis
{
	private static final int TEXT_COLOR = 0xFF999999;

	//画坐标桌上面的点
	@Override
	public void drawAxis(Canvas pCanvas, Paint pPaint)
	{
		RectF tChartRect = m_callback.getChartRectF();
		CoinChart.PointLocation tPointlocation = m_callback.getOriginPointLocation();
		List<Point> tArrPointList = m_arrPointList;

		Layer.ChartTypeEnum chartTypeEnum = m_callback.getCharTypeEnum();
		if (tArrPointList != null)
		{
			float tTempXPosition = -1000;
			Point tPoint;
			int size = tArrPointList.size();
			for (int i = 0; i < size; i++)
			{
				tPoint = tArrPointList.get(i);
				boolean shouldDrawPath = m_bIsShowCoordinate
						&& ((chartTypeEnum != Layer.ChartTypeEnum.DAILY && chartTypeEnum != Layer.ChartTypeEnum.DAILYBAR)
						|| (chartTypeEnum == Layer.ChartTypeEnum.DAILY || chartTypeEnum == Layer.ChartTypeEnum.DAILYBAR) &&  i != 0 && i != size - 1);//分时的第一根和最后一根虚线不画
				if (shouldDrawPath)
				{
					pPaint.reset();
					pPaint.setColor(CoinChart.getBorderColor());

					pPaint.setStrokeWidth(1.f);
					pPaint.setAntiAlias(true);
					pPaint.setStyle(Paint.Style.STROKE);
//					PathEffect effects = new DashPathEffect(new float[]
//					{ 5, 5, 5, 5 }, 5);
//					pPaint.setPathEffect(effects);

					Path tPath = new Path();
					tPath.moveTo(tPoint.getStartX(), tPoint.getStartY());//移动到起始点
					tPath.lineTo(tPoint.getEndX(), tPoint.getEndY());//画线到重点
					pCanvas.drawPath(tPath, pPaint);
				}

				pPaint.reset();
				if (m_bIsShowAxis)
				{
					pPaint.setAntiAlias(true);
					pPaint.setColor(TEXT_COLOR);
					pPaint.setTextSize(m_fTextSize);
					float tTextWidth = pPaint.measureText(tPoint.getText());
					float tXPosition = tPoint.getStartX() - tTextWidth / 2;
//					float tLfetPosition = m_callback.getPanelRect().left;
//					tXPosition = tXPosition >= tLfetPosition ? tXPosition : (tLfetPosition + 10);
					float tLfetPosition = m_callback.getChartRectF().left;
					tXPosition = tXPosition >= tLfetPosition ? tXPosition : tLfetPosition;

					if (tXPosition +tTextWidth > m_callback.getChartRectF().right)
					{
						tXPosition = m_callback.getChartRectF().right - tTextWidth;
					}
					
					if (tXPosition > tTempXPosition)
					{
						tTempXPosition = tXPosition + tTextWidth;
						
						if (tPointlocation == CoinChart.PointLocation.BOTTOMLEFT || tPointlocation == CoinChart.PointLocation.BOTTOMRIGHT)
						{
							pCanvas.drawText(tPoint.getText(), tXPosition, tChartRect.bottom + (m_callback.getXTextSpace() + m_fTextSize) / 2, pPaint);
						}
						else
						{
							pCanvas.drawText(tPoint.getText(), tXPosition, tChartRect.top - (m_callback.getXTextSpace() - m_fTextSize) / 2, pPaint);
						}
					}
				}
			}
		}
	}

	@Override
	public void drawAxisText(Canvas pCanvas, Paint pPaint)
	{

	}

	@Override
	public void drawAxis(Canvas pCanvas, int startXTagIndex, int onePageTagCount, Paint pPaint)
	{
		RectF tChartRect = m_callback.getChartRectF();
		CoinChart.PointLocation tPointlocation = m_callback.getOriginPointLocation();
		List<Point> tArrPointList = m_arrPointList;
		if (tArrPointList != null)
		{
			float tTempXPosition = -1000;
			Point tPoint;
			int max = Math.min(startXTagIndex + onePageTagCount,m_arrPointList.size());
			for (int i = startXTagIndex; i < max; i++)
			{
				tPoint = m_arrPointList.get(i);
				if (m_bIsShowCoordinate)
				{
					pPaint.reset();
					pPaint.setColor(Color.LTGRAY);
					pPaint.setStyle(Paint.Style.STROKE);
					pPaint.setStrokeWidth(2);
					pPaint.setAntiAlias(true);
					PathEffect effects = new DashPathEffect(new float[]
							{ 5, 5, 5, 5 }, 5);
					pPaint.setPathEffect(effects);

					Path tPath = new Path();
					tPath.moveTo(tPoint.getStartX(), tPoint.getStartY());//移动到起始点
					tPath.lineTo(tPoint.getEndX(), tPoint.getEndY());//画线到重点
					pCanvas.drawPath(tPath, pPaint);
				}

				pPaint.reset();
				if (m_bIsShowAxis)
				{
					pPaint.setAntiAlias(true);
					pPaint.setColor(TEXT_COLOR);
					pPaint.setTextSize(m_fTextSize);
					float tTextWidth = pPaint.measureText(tPoint.getText());
					float tXPosition = tPoint.getStartX() - tTextWidth / 2;
//					float tLfetPosition = m_callback.getPanelRect().left;
//					tXPosition = tXPosition >= tLfetPosition ? tXPosition : (tLfetPosition + 10);
					float tLfetPosition = m_callback.getChartRectF().left;
					tXPosition = tXPosition >= tLfetPosition ? tXPosition : tLfetPosition;

					if (tXPosition +tTextWidth > m_callback.getChartRectF().right)
					{
						tXPosition = m_callback.getChartRectF().right - tTextWidth;
					}

					if (tXPosition > tTempXPosition)
					{
						tTempXPosition = tXPosition + tTextWidth;

						if (tPointlocation == CoinChart.PointLocation.BOTTOMLEFT || tPointlocation == CoinChart.PointLocation.BOTTOMRIGHT)
						{
							pCanvas.drawText(tPoint.getText(), tXPosition, tChartRect.bottom + (m_callback.getXTextSpace() + m_fTextSize) / 2, pPaint);
						}
						else
						{
							pCanvas.drawText(tPoint.getText(), tXPosition, tChartRect.top - (m_callback.getXTextSpace() - m_fTextSize) / 2, pPaint);
						}
					}
				}
			}




		}
	}
}
